import axios from 'axios'
import { Message } from 'element-ui'
import store from '@/store'
const dashboardRequest = axios.create({
  baseURL: '/api'
})

dashboardRequest.interceptors.request.use((config) => {
  config.headers.Authorization = store.getters.token
  return config
}, err => {
  console.log(err)
  return Promise.reject(err)
})
dashboardRequest.interceptors.response.use((response) => {
  // console.log(response)
  return response.data
}, err => {
  if (err.response.status === 401) {
    Message.error('token失效，请重新登录')
    store.dispatch('user/logout')
  } else {
    Message.error(err.response.data)
  }

  return Promise.reject(err)
})
export default dashboardRequest
